package com.mall.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.model.Address;
import com.mall.model.User;
import com.mall.service.AddressService;
import com.mall.util.MyUtil;

@Controller
@RequestMapping("/AddressController")
public class AddressController extends MyUtil {
	@Autowired
	private AddressService addressService;

	@RequestMapping("/addAddress")
	private void addAddress() {

	}

	/**
	 * 异步获取地址,根据id
	 *
	 * @dagewang 2017年9月6日
	 */
	@RequestMapping("/getAddress")
	public @ResponseBody Map<String, Object> getAddress(Integer id) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		if (id == null) {
			map.put("address", null);
		}
		Address a = addressService.get(id, null).get(0);
		map.put("address", a);
		return map;

	}

	/**
	 * 根据userID获取地址信息
	 *
	 * @dagewang 2017年9月13日
	 */
	@RequestMapping("/listAddress")
	public String listAddress(HttpSession session, Model model,String error) throws Exception {
		User user = (User) session.getAttribute("user");
		if (user == null) {
			return "login";
		}
		if(!(error==null||error.equals(""))){
			model.addAttribute("error", error);
		}
		List<Address> list = addressService.getByUserId(user.getId());
		if (list.size() == 0) {
			model.addAttribute("addresslist", null);
		}
		model.addAttribute("addresslist", list);
		return "userCenterAddress";

	}

	/**
	 * 异步修改地址
	 * 
	 * @throws Exception
	 *
	 * @dagewang 2017年9月13日
	 */
	@RequestMapping("/update")
	public @ResponseBody Integer update(Address address,Integer did) throws Exception {
		if (this.isNull(address.getName(), address.getAddress(), address.getPhonenum())) {
			return 0;
		}
		Integer result = addressService.update(address,did);
		return result;

	}
	/**
	 * 添加地址
	 * @throws Exception 
	 *
	 *@dagewang 2017年9月13日
	 */
	@RequestMapping("/saveAddress")
	public String savaAddress(Address address,HttpSession session,Model model) throws Exception{
		User user=(User)session.getAttribute("user");
		if(user!=null){
			if(this.isNull(address.getAddress(),address.getName(),address.getPhonenum())){
				return "redirect:listAddress.action?error=e";
			}else{
				List<Address> list = addressService.getByUserId(user.getId());
				Address a=addressService.save(address,user.getId(),list.size());
				if(a==null){
					model.addAttribute("error", "error");
					return "redirect:listAddress.action?error=e";
				}else{
					return "redirect:listAddress.action";
				}
				
			}
		}
		
		return "login";
	}

	public AddressService getAddressService() {
		return addressService;
	}

	public void setAddressService(AddressService addressService) {
		this.addressService = addressService;
	}

}
